package com.customization.unionpay.market.workflowaction;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.util.CloudFlashPaymentUtils;
import com.customization.unionpay.market.util.FormDataUtils;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.OverTimeSetBean;
import weaver.workflow.request.RequestManager;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author sjm
 * @description 营销事项申请表单节点(云闪付)action
 */
public class MarketingApplicationYunAction implements Action {

    private static final Logger log = LoggerFactory.getLogger(MarketingApplicationYunAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {

        OverTimeSetBean overTimeBean = new OverTimeSetBean();

        RequestManager rm = requestInfo.getRequestManager();

        try {
            ResourceComInfo rci = new ResourceComInfo();
            SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();

            int nodeId = rm.getNodeid();
            // 流程表单id
            int formId = rm.getFormid();

            int user = rm.getUser().getUID();                  // 当前操作者
            String tableName = rm.getBillTableName();          // 表单名称
            String requestId = requestInfo.getRequestid();     // 流程id

            String nbyxValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(formId, "zjly", "内部营销");
            log.info("nbyxValue:" + nbyxValue);
            String nwbyxValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(formId, "zjly", "内外部联合营销");
            log.info("nwbyxValue:" + nwbyxValue);

            String jljValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(formId, "ACT_TP", "奖励金");
            log.info("jljValue:" + jljValue);
            // 积分（普通） 积分（消费送红包）
            String jfpt = com.customization.unionpay.market.util.FormDataUtils.getItemValue(formId, "ACT_TP", "积分（普通）");
            log.info("jfpt:" + jfpt);
            String jfsfhb = com.customization.unionpay.market.util.FormDataUtils.getItemValue(formId, "ACT_TP", "积分（消费送红包）");
            log.info("jfsfhb:" + jfsfhb);

            Map<String, Object> allFormData = com.customization.unionpay.market.util.FormDataUtils.getAllFormData(requestInfo);

            Map<String, String> mainMap = (Map<String, String>) allFormData.get("mainMap");
            // 所有明细表的值
            Map<String, Map<String, Map<String, String>>> detailMap = (Map<String, Map<String, Map<String, String>>>) allFormData.get("detailMap");

            Map<String, Map<String, String>> dt1 = detailMap.get("dt1");

            String nodeName = overTimeBean.getNodeName(nodeId);
            log.info("nodeName:" + nodeName);
            // 请求数据
            JSONObject createDatas = new JSONObject();
            String actStartTm = "";
            String actEndTm = "";
            String nbczje = "";
            // 明细只有一行数据
            for (String key : dt1.keySet()) {
                Map<String, String> value = dt1.get(key);
                // 内部出资金额
                nbczje = com.customization.unionpay.market.util.FormDataUtils.changeNumber(value.get("nbczje"));
                // 活动开始时间
                actStartTm = value.get("act_start_tm");
                // 活动结束时间
                actEndTm = value.get("act_end_tm");
            }
            // 申请人
            String sqr = mainMap.get("sqr");
            // 报账部门
            String bzbm = mainMap.get("bzbm");
            log.info("bzbm:" + bzbm);
            // 内部 、内外部、外部
            String zjly = mainMap.get("zjly");
            // 项目名称
            String project_name = mainMap.get("project_name");
            // 申请人名称
            String sqrName = rci.getLastname(sqr);
            log.info("sqrName:" + sqrName);
            // 立减 积分（普通） 积分（消费送红包），奖励金(等于立减)
            String actTp = mainMap.get("act_tp");
            log.info("actTp:" + actTp);
            // 营销平台备注
            String yxptbz = mainMap.get("yxptbz").trim();
            log.info("yxptbz:" + yxptbz);
            // 项目名称
            createDatas.put("projectName", project_name);
            // 结算方式  0-事后入账 1-全额入账
            createDatas.put("clearForm", "1");
            // 项目开始日期  YYYY-MM-DD
            createDatas.put("projectStartDate", actStartTm);
            // 项目结束日期  YYYY-MM-DD
            createDatas.put("projectEndDate", actEndTm);

            String projectBusiType = "0";
            // 项目业务类型    立减活动-“0” 积分（普通-“1” 积分（消费送红包）-“2” 代金券-“3” 其他-“99”;
            if(actTp.equals(jljValue)){
                projectBusiType = "3";
            }else if(actTp.equals(jfpt)){
                projectBusiType = "1";
            }else if(actTp.equals(jfsfhb)){
                projectBusiType = "2";
            }
            createDatas.put("projectBusiType", projectBusiType);
            // 其他项目业务类型 选填，项目业务类型选择其他-99时必填。项目业务类型不为其他时不能填写
            createDatas.put("otherBusiType", "");
            // 项目充值金额
            createDatas.put("budgetValue", nbczje);
            // 授权码出资类型      内部=”1” 外部=”2” 内外联合=”3”
            String contributionType = "2";
            if(zjly.equals(nbyxValue)){
                contributionType = "1";
            }else if(zjly.equals(nwbyxValue)){
                contributionType = "3";
            }
            createDatas.put("contributionType", contributionType);
            // 银联出资金额          授权码出资类型为3时必填
            createDatas.put("unionpayValue", nbczje);
            // 申请人
            createDatas.put("applicantUserId", sqrName);
            // 报账公司
            String bzgs = mainMap.get("bzgs");
            String codeByDepartmentId = com.customization.unionpay.market.util.FormDataUtils.getCodeByDepartmentId(bzgs);
            String subCompanyname = subCompanyComInfo.getSubCompanyname(bzgs);
            log.info("subCompanyname:" + subCompanyname);

            // 申请人分公司/部门
            createDatas.put("applicantOrgId", codeByDepartmentId);
            // 预算授权码类型    0-实体 1-虚体
            createDatas.put("budgetAuthCodeType", "0");
            // 项目材料上传  非必填
            createDatas.put("filesId", "");
            // 备注 remark
            createDatas.put("remark", "");
            log.info("createDatas:" + createDatas);

            JSONObject changeDatas = new JSONObject();
            // 调整原因	     data.adjustReason	String	1024		必填
            changeDatas.put("adjustReason", yxptbz+"调整金额");
            // 调整类型	     data.adjustType	String	1		        必填    0-增加  1-减少  2-退款
            changeDatas.put("adjustType", "0");

            // 调整金额	     data.adjustValue	decimal(12,2)			必填
            changeDatas.put("adjustValue", nbczje);
            // 待审核资金量	 data.fundValue				必填      该值为已审核资金量值加上调整金额的量
            changeDatas.put("fundValue", "");
            // 预算授权码	     data.budgetAuthCode	String	30		必填
            changeDatas.put("budgetAuthCode",yxptbz);

            // 银联出资调整金额 data.adjustUnionpayValue	decimal(12,2) 必填。 内外联合类型的预算授权码该值不能大于调整金额的值。非内外联合类型的预算授权码该值该值传递0
            if(zjly.equals(nbyxValue)){
                changeDatas.put("adjustUnionpayValue", "0");
            }else if(zjly.equals(nwbyxValue)){
                changeDatas.put("adjustUnionpayValue", nbczje);
            }

            // 内部出资金额
            Double nbczjeD = new BigDecimal(nbczje).doubleValue();
            log.info("nbczjeD:" + nbczjeD);
            // 调用云闪付,动作失败回退指定节点 奖励金算立减  （立减，积分（普通），积分（消费送红包），奖励金）
            if(!subCompanyname.contains("总公司")) {
                // 分公司
                log.info("进入分公司");
                if ("核算会计初审".equals(nodeName)) {
                    log.info("nodeName:hskjcs");
                    if (zjly.equals(nbyxValue)) {
                        // 内部营销   【活动类型】=立减 【活动类型】=积分  新增授权码和授权码金额充值
                        try {
                            String innerGenerateByBudgetCode = com.customization.unionpay.market.util.CloudFlashPaymentUtils.innerGenerateByBudgetCode(createDatas);
                            log.info("innerGenerateByBudgetCode:" + innerGenerateByBudgetCode);

                            JSONObject resultJson = JSONObject.parseObject(innerGenerateByBudgetCode);

                            String errorMessage = resultJson.getString("errorMessage");
                            if ("成功".equals(errorMessage)) {
                                String data = resultJson.getString("data");
                                com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(data, tableName, requestId);
                            } else {
                                log.error("errorMessage:" + errorMessage);
                                com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
//                                FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                requestInfo.getRequestManager().setMessageid("10001");
                                requestInfo.getRequestManager().setMessagecontent(errorMessage);
                                return Action.FAILURE_AND_CONTINUE;
                            }
                        } catch (Exception e) {
                            log.error("e.getMessage():" + e.getMessage());
                            com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
//                            FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                            requestInfo.getRequestManager().setMessageid("10001");
                            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                            return Action.FAILURE_AND_CONTINUE;
                        }
                    }

                    if (zjly.equals(nwbyxValue)) {
                        if (!"".equals(yxptbz)) {
                            // 内外部营销 【活动类型】=立减 【活动类型】=积分  如填授权码或已有授权码（营销平台备注）   授权码金额充值
                            // fundValueChecked-剩余资金量 +/-  bcdzje    nbczje
                            List<String> codes = new ArrayList<>();
                            codes.add(yxptbz);
                            // 云闪付查询
                            try {
                                String resultStr = com.customization.unionpay.market.util.CloudFlashPaymentUtils.batchQueryByBudgetAuthCode(codes);
                                log.info("resultStr：" + resultStr);
                                JSONObject resultJson = JSONObject.parseObject(resultStr);
                                String errorMessage = resultJson.getString("errorMessage");
                                if ("成功".equals(errorMessage)) {
                                    JSONArray data = resultJson.getJSONArray("data");
                                    log.info("dataParams：" + data);
                                    if (data.size() > 0) {
                                        JSONObject single = data.getJSONObject(0);
                                        // 剩余资金
                                        Double fundValueChecked = single.getDouble("fundValueChecked");
                                        Double nbje = Double.valueOf(nbczje);
                                        String fundValue = com.customization.unionpay.market.util.FormDataUtils.changeNumber(String.valueOf((fundValueChecked + nbje)));
                                        changeDatas.put("fundValue", fundValue);
                                        // 云闪付调整
                                        try {
                                            String innerAdjustByBudgetCode = com.customization.unionpay.market.util.CloudFlashPaymentUtils.innerAdjustByBudgetCode(changeDatas);
                                            log.info("innerAdjustByBudgetCode：" + innerAdjustByBudgetCode);
                                            JSONObject innerAdjustByBudgetCodeJson = JSONObject.parseObject(innerAdjustByBudgetCode);
                                            String innerAdjustErrorMessage = innerAdjustByBudgetCodeJson.getString("errorMessage");
                                            if ("成功".equals(innerAdjustErrorMessage)) {
                                                com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(yxptbz, tableName, requestId);
                                            } else {
                                                // 云闪付调整失败
                                                log.error("yunErrorCode:" + resultJson.getString("errorCode") + "----" + innerAdjustErrorMessage);
                                                com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(innerAdjustErrorMessage, tableName, requestId);
//                                                FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                                requestInfo.getRequestManager().setMessageid("10001");
                                                requestInfo.getRequestManager().setMessagecontent(innerAdjustErrorMessage);
                                                return Action.FAILURE_AND_CONTINUE;
                                            }
                                        } catch (Exception e) {
                                            // 云闪付调整异常
                                            log.error("yunError:" + e.getMessage());
                                            com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
//                                            FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                            requestInfo.getRequestManager().setMessageid("10001");
                                            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                                            return Action.FAILURE_AND_CONTINUE;
                                        }
                                    } else {
                                        // 云闪付查询失败
                                        log.error("yunSelectErrorCode: 云闪付根据授权码查询为空");
                                        com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo("云闪付根据授权码查询为空", tableName, requestId);
//                                        FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                        requestInfo.getRequestManager().setMessageid("10001");
                                        requestInfo.getRequestManager().setMessagecontent("云闪付根据授权码查询为空");
                                        return Action.FAILURE_AND_CONTINUE;
                                    }
                                } else {
                                    // 云闪付查询失败
                                    log.error("yunSelectErrorCode:" + resultJson.getString("errorCode") + "----" + errorMessage);
                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
//                                    FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                    requestInfo.getRequestManager().setMessageid("10001");
                                    requestInfo.getRequestManager().setMessagecontent(errorMessage);
                                    return Action.FAILURE_AND_CONTINUE;
                                }
                            } catch (Exception e) {
                                // 云闪付查询异常
                                log.error("yunSelectError:" + e.getMessage());
                                com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
//                                FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                requestInfo.getRequestManager().setMessageid("10001");
                                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                                return Action.FAILURE_AND_CONTINUE;
                            }
                        } else {
                            //                                         如不填授权码                        新增授权码和授权码金额充值
                            try {
                                String innerGenerateByBudgetCode = com.customization.unionpay.market.util.CloudFlashPaymentUtils.innerGenerateByBudgetCode(createDatas);
                                log.info("innerGenerateByBudgetCode:" + innerGenerateByBudgetCode);

                                JSONObject resultJson = JSONObject.parseObject(innerGenerateByBudgetCode);

                                String errorMessage = resultJson.getString("errorMessage");
                                if ("成功".equals(errorMessage)) {
                                    String data = resultJson.getString("data");
                                    com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(data, tableName, requestId);
                                } else {
                                    // 云闪付生成授权码失败
                                    log.error("yunCreateErrorCode:" + errorMessage);
                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
//                                    FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                    requestInfo.getRequestManager().setMessageid("10001");
                                    requestInfo.getRequestManager().setMessagecontent(errorMessage);
                                    return Action.FAILURE_AND_CONTINUE;
                                }
                            } catch (Exception e) {
                                // 云闪付生成授权码异常
                                log.error("yunCreateError:" + e.getMessage());
                                com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
//                                FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                requestInfo.getRequestManager().setMessageid("10001");
                                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                                return Action.FAILURE_AND_CONTINUE;
                            }
                        }
                    }
                }
            }else {
                // 总公司
                log.info("进入总公司");
                if (nbczjeD > 100000) {
                    // 项目金额 > 100000
                    if ("公司分管财务领导审批".equals(nodeName)) {
                        log.info("nodeName:gsfgcwldsp");
                        if (zjly.equals(nbyxValue)) {
                            // 内部营销   【活动类型】=立减 【活动类型】=积分  新增授权码和授权码金额充值
                            try {
                                String innerGenerateByBudgetCode = com.customization.unionpay.market.util.CloudFlashPaymentUtils.innerGenerateByBudgetCode(createDatas);
                                log.info("innerGenerateByBudgetCode:" + innerGenerateByBudgetCode);

                                JSONObject resultJson = JSONObject.parseObject(innerGenerateByBudgetCode);

                                String errorMessage = resultJson.getString("errorMessage");
                                if ("成功".equals(errorMessage)) {
                                    String data = resultJson.getString("data");
                                    com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(data, tableName, requestId);
                                } else {
                                    // 云闪付生成授权码失败
                                    log.error("yunCreateErrorCode:" + errorMessage);
                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
                                    com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                }
                            } catch (Exception e) {
                                // 云闪付生成授权码异常
                                log.error("yunCreateError:" + e.getMessage());
                                com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
                                com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                            }
                        }

                        if (zjly.equals(nwbyxValue)) {
                            if (!"".equals(yxptbz)) {
                                // 内外部营销 【活动类型】=立减 【活动类型】=积分  如填授权码或已有授权码（营销平台备注）   授权码金额充值
                                // fundValueChecked-剩余资金量 +/-  bcdzje    nbczje
                                List<String> codes = new ArrayList<>();
                                codes.add(yxptbz);
                                // 云闪付查询
                                try {
                                    String resultStr = com.customization.unionpay.market.util.CloudFlashPaymentUtils.batchQueryByBudgetAuthCode(codes);
                                    log.info("resultStr：" + resultStr);
                                    JSONObject resultJson = JSONObject.parseObject(resultStr);
                                    String errorMessage = resultJson.getString("errorMessage");
                                    if ("成功".equals(errorMessage)) {
                                        JSONArray data = resultJson.getJSONArray("data");
                                        log.info("dataParams：" + data);
                                        if (data.size() > 0) {
                                            JSONObject single = data.getJSONObject(0);
                                            // 剩余资金
                                            Double fundValueChecked = single.getDouble("fundValueChecked");
                                            Double nbje = Double.valueOf(nbczje);
                                            String fundValue = com.customization.unionpay.market.util.FormDataUtils.changeNumber(String.valueOf((fundValueChecked + nbje)));
                                            changeDatas.put("fundValue", fundValue);
                                            // 云闪付调整
                                            try {
                                                String innerAdjustByBudgetCode = com.customization.unionpay.market.util.CloudFlashPaymentUtils.innerAdjustByBudgetCode(changeDatas);
                                                log.info("innerAdjustByBudgetCode：" + innerAdjustByBudgetCode);
                                                JSONObject innerAdjustByBudgetCodeJson = JSONObject.parseObject(innerAdjustByBudgetCode);
                                                String innerAdjustErrorMessage = innerAdjustByBudgetCodeJson.getString("errorMessage");
                                                if ("成功".equals(innerAdjustErrorMessage)) {
                                                    com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(yxptbz, tableName, requestId);
                                                } else {
                                                    // 云闪付调整失败
                                                    log.error("yunErrorCode:" + resultJson.getString("errorCode") + "----" + innerAdjustErrorMessage);
                                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(innerAdjustErrorMessage, tableName, requestId);
                                                    com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                                }
                                            } catch (Exception e) {
                                                // 云闪付调整异常
                                                log.error("yunError:" + e.getMessage());
                                                com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
                                                com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                            }
                                        } else {
                                            // 云闪付查询失败
                                            log.error("yunSelectErrorCode: 云闪付根据授权码查询为空");
                                            com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo("云闪付根据授权码查询为空", tableName, requestId);
                                            com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                        }
                                    } else {
                                        // 云闪付查询失败
                                        log.error("yunSelectErrorCode:" + resultJson.getString("errorCode") + "----" + errorMessage);
                                        com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
                                        com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                    }
                                } catch (Exception e) {
                                    // 云闪付查询异常
                                    log.error("yunSelectError:" + e.getMessage());
                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
                                    com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                }
                            } else {
                                //                                         如不填授权码                        新增授权码和授权码金额充值
                                try {
                                    String innerGenerateByBudgetCode = com.customization.unionpay.market.util.CloudFlashPaymentUtils.innerGenerateByBudgetCode(createDatas);
                                    log.info("innerGenerateByBudgetCode：" + innerGenerateByBudgetCode);

                                    JSONObject resultJson = JSONObject.parseObject(innerGenerateByBudgetCode);

                                    String errorMessage = resultJson.getString("errorMessage");
                                    if ("成功".equals(errorMessage)) {
                                        String data = resultJson.getString("data");
                                        com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(data, tableName, requestId);
                                    } else {
                                        // 云闪付生成授权码失败
                                        log.error("yunCreateErrorCode:" + errorMessage);
                                        com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
                                        com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                    }
                                } catch (Exception e) {
                                    // 云闪付生成授权码异常
                                    log.error("yunCreateError:" + e.getMessage());
                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
                                    com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                }
                            }
                        }
                    }
                } else {
                    // 项目金额 < 100000
                    if ("财务部总经理室审批".equals(nodeName)) {
                        log.info("nodeName:cwbzjlssp");
                        if (zjly.equals(nbyxValue)) {
                            // 内部营销   【活动类型】=立减 【活动类型】=积分  新增授权码和授权码金额充值
                            try {
                                String innerGenerateByBudgetCode = com.customization.unionpay.market.util.CloudFlashPaymentUtils.innerGenerateByBudgetCode(createDatas);
                                log.info("innerGenerateByBudgetCode：" + innerGenerateByBudgetCode);

                                JSONObject resultJson = JSONObject.parseObject(innerGenerateByBudgetCode);

                                String errorMessage = resultJson.getString("errorMessage");
                                if ("成功".equals(errorMessage)) {
                                    String data = resultJson.getString("data");
                                    com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(data, tableName, requestId);
                                } else {
                                    // 云闪付生成授权码失败
                                    log.error("yunCreateErrorCode:" + errorMessage);
                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
                                    com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                }
                            } catch (Exception e) {
                                // 云闪付生成授权码异常
                                log.error("yunCreateError:" + e.getMessage());
                                com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
                                com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                            }
                        }

                        if (zjly.equals(nwbyxValue)) {
                            if (!"".equals(yxptbz)) {
                                // 内外部营销 【活动类型】=立减 【活动类型】=积分  如填授权码或已有授权码（营销平台备注）   授权码金额充值
                                // fundValueChecked-剩余资金量 +/-  bcdzje    nbczje
                                List<String> codes = new ArrayList<>();
                                codes.add(yxptbz);
                                // 云闪付查询
                                try {
                                    String resultStr = com.customization.unionpay.market.util.CloudFlashPaymentUtils.batchQueryByBudgetAuthCode(codes);
                                    log.info("resultStr：" + resultStr);
                                    JSONObject resultJson = JSONObject.parseObject(resultStr);
                                    String errorMessage = resultJson.getString("errorMessage");
                                    if ("成功".equals(errorMessage)) {
                                        JSONArray data = resultJson.getJSONArray("data");
                                        log.info("dataParams：" + data);

                                        JSONObject single = data.getJSONObject(0);
                                        // 剩余资金
                                        Double fundValueChecked = single.getDouble("fundValueChecked");
                                        Double nbje = Double.valueOf(nbczje);
                                        String fundValue = com.customization.unionpay.market.util.FormDataUtils.changeNumber(String.valueOf((fundValueChecked + nbje)));
                                        changeDatas.put("fundValue", fundValue);
                                        // 云闪付调整
                                        try {
                                            String innerAdjustByBudgetCode = com.customization.unionpay.market.util.CloudFlashPaymentUtils.innerAdjustByBudgetCode(changeDatas);
                                            log.info("innerAdjustByBudgetCode：" + innerAdjustByBudgetCode);
                                            JSONObject innerAdjustByBudgetCodeJson = JSONObject.parseObject(innerAdjustByBudgetCode);
                                            String innerAdjustErrorMessage = innerAdjustByBudgetCodeJson.getString("errorMessage");
                                            if ("成功".equals(innerAdjustErrorMessage)) {
                                                com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(yxptbz, tableName, requestId);
                                            } else {
                                                // 云闪付调整失败
                                                log.error("yunErrorCode:" + resultJson.getString("errorCode") + "----" + innerAdjustErrorMessage);
                                                com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(innerAdjustErrorMessage, tableName, requestId);
                                                com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                            }
                                        } catch (Exception e) {
                                            // 云闪付调整异常
                                            log.error("yunError:" + e.getMessage());
                                            com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
                                            com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                        }
                                    } else {
                                        // 云闪付查询失败
                                        log.error("yunSelectErrorCode:" + resultJson.getString("errorCode") + "----" + errorMessage);
                                        com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
                                        com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                    }
                                } catch (Exception e) {
                                    // 云闪付查询异常
                                    log.error("yunSelectError:" + e.getMessage());
                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
                                    com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                }
                            } else {
                                //                                         如不填授权码                        新增授权码和授权码金额充值
                                try {
                                    String innerGenerateByBudgetCode = CloudFlashPaymentUtils.innerGenerateByBudgetCode(createDatas);
                                    log.info("innerGenerateByBudgetCode：" + innerGenerateByBudgetCode);

                                    JSONObject resultJson = JSONObject.parseObject(innerGenerateByBudgetCode);

                                    String errorMessage = resultJson.getString("errorMessage");
                                    if ("成功".equals(errorMessage)) {
                                        String data = resultJson.getString("data");
                                        com.customization.unionpay.market.util.FormDataUtils.changeSqmInfo(data, tableName, requestId);
                                    } else {
                                        // 云闪付生成授权码失败
                                        log.error("yunCreateErrorCode:" + errorMessage);
                                        com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(errorMessage, tableName, requestId);
                                        com.customization.unionpay.market.util.FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                    }
                                } catch (Exception e) {
                                    // 云闪付生成授权码异常
                                    log.error("yunCreateError:" + e.getMessage());
                                    com.customization.unionpay.market.util.FormDataUtils.changeErrorInfo(e.getMessage(), tableName, requestId);
                                    FormDataUtils.rejectRequest(Integer.parseInt(requestId), user);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            // 异常报错是填写异常信息，阻止流程继续流转
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
